/*
 *  CEModalView.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/controls/CEModalView.h"

#include "cocos2dx_extentions/utility/CECocos2dUtility.h"
#include "lid_lib/src/data_managment/LLNotifier.h"

using namespace cocos2d;
using namespace lidlib;

namespace cocosext {

/////////////////////////////////////////////////////
//
/** CEModalView 
* ModalView is a full screen modal wnd Layer that has a high (CE_MODAL_VIEW_TOUCH_PRIORITY) touch priority.
* When it starts, it sends kCEOnModalViewStartOpen notification  (this is a parameter)
* When it stops, it sends kCEOnModalViewFinishClose notification  (this is a parameter)
*/
/////////////////////////////////////////////////////
/*
bool CEModalView::createAndAttachToCurrentScene(const ccColor4B& color)
{
	CEModalView *pRet = new CEModalView();
    CCAssert(pRet != NULL, NULL);
    if (pRet->init(color))
    {
        pRet->autorelease();
		CCDirector::sharedDirector()->getRunningScene()->addChild(pRet, INT_MAX);
        return true;
    }
    else
    {
        CC_SAFE_DELETE(pRet)
        return false;
    }
}
*/

/////////////////////////////////////////////////////
// Constructor, destructor, init

bool CEModalView::init(const ccColor4B& color)
{
	if (!CCLayerColor::initWithColor(color))
		return false;

	_resultAlpha = color.a;
	this->setAnchorPoint(CCPointZero);
	this->setPosition(CCPointZero);
	
	this->setOpacity(0);
	if (_autoShowHide)
		this->setIsVisible(false);

	this->setIsTouchEnabled(false);
	_isResizeBlocked = true;

	return true;
}

/////////////////////////////////////////////////////
// Main interface


/////////////////////////////////////////////////////
//Cocos2d interface

void CEModalView::setRotation(float fRotation)
{
	CCAssert(false, "CEModalView::setRotation: CEModalView node can't be rotated!");
}
void CEModalView::setContentSize(const cocos2d::CCSize& contentSize)
{
	if (_isResizeBlocked)
	{
		CCAssert(false, "CEModalView::setContentSize: CEModalView node can't be resized!");
	}
	else
		CCLayerColor::setContentSize(contentSize);
}
void CEModalView::registerWithTouchDispatcher()
{
	CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, CE_MODAL_VIEW_TOUCH_PRIORITY, true);
}
	
/////////////////////////////////////////////////////
// Touch processing
bool CEModalView::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    CCObject* child(NULL);
	CCARRAY_FOREACH(this->getChildren(), child)
    {
        CCNode* pNode = (CCNode*) child;
		if (isNodeContainsTouch(*pNode, pTouch))
		{
			_isChildContainsBeginTouch = true;
			break;
		}
	}

	return true;
}

void CEModalView::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}

void CEModalView::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
	if (_isChildContainsBeginTouch)
		_isChildContainsBeginTouch = false;
	else if (!_isTouchProcessingBlocked)
	{
		CCObject* child(NULL);
		CCARRAY_FOREACH(this->getChildren(), child)
		{
			CCNode* pNode = (CCNode*) child;
			if (isNodeContainsTouch(*pNode, pTouch))
			{
				_isChildContainsBeginTouch = true;
				break;
			}
		}
		if (_isChildContainsBeginTouch)
			_isChildContainsBeginTouch = false;
		else
		{
			close();
		}
	}
}

void CEModalView::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}

/////////////////////////////////////////////////////
// Protected interface

void CEModalView::open()
{
	if (_autoShowHide)
		this->setIsVisible(true);
	this->runAction(CCSequence::actions(CCFadeTo::actionWithDuration(CE_MODAL_VIEW_FADE_TIME, 255 - _resultAlpha),
										CCCallFunc::actionWithTarget(this, callfunc_selector(CEModalView::finishOpen)), NULL));
	if (_sendNotifications)
		LLNotifier::sendNotification(kCEOnModalViewStartOpen, this);
	this->setIsTouchEnabled(true);
}

void CEModalView::finishOpen()
{
	if (_sendNotifications)
		LLNotifier::sendNotification(kCEOnModalViewFinishOpen, this);
	_isTouchProcessingBlocked = false;
}

void CEModalView::close()
{
	_isTouchProcessingBlocked = true;
	if (_sendNotifications)
		LLNotifier::sendNotification(kCEOnModalViewStartClose, this);
	this->runAction(CCSequence::actionOneTwo(CCFadeTo::actionWithDuration(CE_MODAL_VIEW_FADE_TIME, 0),
											 CCCallFunc::actionWithTarget(this, callfunc_selector(CEModalView::finishClose))));	
	this->setIsTouchEnabled(false);
}

void CEModalView::finishClose()
{
	if (_sendNotifications)
		LLNotifier::sendNotification(kCEOnModalViewFinishClose, this);
	if (_autoShowHide)
		this->setIsVisible(false);
}

}; // end namespace cocosext

